package development;

import java.util.Hashtable;


import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.Graphics;
import state.Key;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.Color;

public class FrmMenu extends GameForm {
  private final int MENU_DY = 20;
  private final int MENU_ITEM_WIDTH = 170;
  private final int MENU_ITEM_HEIGHT = 25;
  private final int MENU_ITEM_VER_DIS = 10;
  
  private final int MENU_BG_COLOR = Color.WHITE_CODE;
  private final int MENU_FG_COLOR = Color.BLACK_CODE;
  private final int MENU_FOCUS_BG_COLOR = Color.GREEN_CODE;
  private final int MENU_FOCUS_FG_COLOR = Color.WHITE_CODE;
  private final int MENU_BORDER_COLOR = Color.RED_CODE;
  
  private String[] MENU;
  private ImageFont text8;
  private int chooseIndex;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmMenu();
    }
    return singleton;
  }
  
  public void init(Hashtable parameters) {
    manager.setBackGroundColor(Color.BLACK_CODE);
    
    MENU = new String[] {"Test Bottom bar", "Test checkbox component", "Test Custom alert", "Test touch TextField", "Test font scroll", "Test resize image", "List file", "Exit"};
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    chooseIndex = 0;
  }
  
  public void draw(Graphics g) {
    int menuDx = (GameConstants.SCREEN_WIDTH - MENU_ITEM_WIDTH) / 2;
    for (int i = 0; i < MENU.length; i++) {
      if (i == chooseIndex) {
        g.setColor(MENU_FOCUS_BG_COLOR);
      } else {
        g.setColor(MENU_BG_COLOR);
      }
      g.fillRect(menuDx, MENU_DY + (MENU_ITEM_HEIGHT + MENU_ITEM_VER_DIS) * i, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT);
      g.setColor(MENU_BORDER_COLOR);
      g.drawRect(menuDx, MENU_DY + (MENU_ITEM_HEIGHT + MENU_ITEM_VER_DIS) * i, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT);
      
      if (i == chooseIndex) {
        text8.drawString(g, MENU[i], MENU_FOCUS_FG_COLOR, menuDx + MENU_ITEM_WIDTH / 2, MENU_DY + (MENU_ITEM_HEIGHT + MENU_ITEM_VER_DIS) * i + MENU_ITEM_HEIGHT / 2, GameConstants.CENTER_ANCHOR);  
      } else {
        text8.drawString(g, MENU[i], MENU_FG_COLOR, menuDx + MENU_ITEM_WIDTH / 2, MENU_DY + (MENU_ITEM_HEIGHT + MENU_ITEM_VER_DIS) * i + MENU_ITEM_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      }
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      chooseIndex = (chooseIndex + 1) % MENU.length;
      break;
    case Key.UP:
      chooseIndex = (--chooseIndex) < 0 ? (MENU.length - 1) : chooseIndex;
      break;
    case Key.FIRE:
      switch (chooseIndex) {
      case 0:
        GameGlobal.nextState(FrmTestBottomBar.instance(), null);
        break;
      case 1:
        GameGlobal.nextState(FrmTestCheckbox.instance(), null);
        break;
      case 2:
        GameGlobal.nextState(FrmTestCustomeAlertUI.instance(), null);
        break;
      case 3:
        GameGlobal.nextState(FrmTestTouchTextField.instance(), null);
        break;
      case 4:
        GameGlobal.nextState(FrmGuide.instance(), null);
        break;
      case 5:
        GameGlobal.nextState(FrmTestResizeImage.instance(), null);
        break;
      case 6:
        GameGlobal.nextState(FrmListFile.instance(), null);
        break;
      case 7: // Exit
        GameGlobal.getMidlet().notifyDestroyed();
        break;
      }
      break;
    }
  }
  
  protected void destroy() {
    MENU = null;
    text8 = null;
  }
}
